/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tools;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;

/**
 * Projet BI - M2 FIC 2012-2013
 *
 * @author Jef, Tuan-Long, William, Mohamed
 */
public class XLSXReader {

    public XLSXReader() {
        // Vide.
    }

    public ArrayList<String> xlsx_getArrayListFromFile(String filePath) {
        ArrayList<String> resAL = new ArrayList();

        try {

            FileInputStream file = new FileInputStream(new File(filePath));

            //Get the workbook instance for XLS file
            HSSFWorkbook workbook = new HSSFWorkbook(file);

            //Get first sheet from the workbook
            HSSFSheet sheet = workbook.getSheetAt(0);

            //Iterate through each rows from first sheet
            Iterator<Row> rowIterator = sheet.iterator();
            while (rowIterator.hasNext()) {
                Row row = rowIterator.next();
                String str_tmp = new String();

                //For each row, iterate through each columns
                Iterator<Cell> cellIterator = row.cellIterator();
                while (cellIterator.hasNext()) {

                    Cell cell = cellIterator.next();

                    switch (cell.getCellType()) {
                        case Cell.CELL_TYPE_BOOLEAN:
                            //System.out.print(cell.getBooleanCellValue() + "\t\t");
                            str_tmp += cell.getBooleanCellValue() + "\t";
                            break;
                        case Cell.CELL_TYPE_NUMERIC:
                            //System.out.print(cell.getNumericCellValue() + "\t\t");
                            str_tmp += cell.getNumericCellValue() + "\t";
                            break;
                        case Cell.CELL_TYPE_STRING:
                            //System.out.print(cell.getStringCellValue() + "\t\t");
                            str_tmp += cell.getStringCellValue() + "\t";
                            break;
                    }
                    
                }
                resAL.add(str_tmp);
                //System.out.println("");
                //System.out.println(str_tmp);
            }
            file.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return resAL;
    }
}
